package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.ResultVO;
import com.project.StringUtil;
import com.project.bean.ClientBean;
import com.project.mapper.ClientDto;
import com.project.service.IClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "客户测试接口")
@RequestMapping("client")
public class ClientController {
    @Autowired
    private IClientService clientService;

    @GetMapping("findByItem/{page}/{pageSize}/{name}/{range}")
    @ApiOperation(value = "按选项分页查找客户")
    public PageInfo<ClientDto> findByItem(@PathVariable("page") int page,
                                          @PathVariable("pageSize") int pageSize,
                                          @PathVariable("name") String name,
                                          @PathVariable("range") String range){
        if(name.equals("$")){
            name = "";
        }
        if(range.equals("$")){
            range = "";
        }
        PageInfo<ClientDto> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(clientService.findByItem(page,pageSize,name,range),pageInfo);
        return pageInfo;
    }

    @PostMapping("add")
    @ApiOperation(value = "添加客户")
    public ResultVO add(@RequestBody ClientBean client){
        ClientBean clientBean = new ClientBean();
        BeanUtils.copyProperties(client,clientBean);
        return ResultVO.getResultVo(clientService.addClient(client),
                StringUtil.ADDCLIENT_OK,
                StringUtil.ADDCLIENT_ERROR_EXIST);
    }

    @GetMapping("findById/{id}")
    @ApiOperation(value = "按id查询客户")
    public ClientDto findById(@PathVariable("id")int id){
        ClientDto clientDto = new ClientDto();
        ClientBean clientBean=clientService.findById(id);
        BeanUtils.copyProperties(clientBean,clientDto);
        return clientDto ;
    }

    @PutMapping("update")
    @ApiOperation(value = "修改客户信息")
    public ResultVO update(@RequestBody ClientDto client){
        ClientBean clientBean = new ClientBean();
        BeanUtils.copyProperties(client,clientBean);
        clientService.updateClient(clientBean);
        return ResultVO.ResultVoOK(StringUtil.UPDATECLIENT_OK);
    }

//    @GetMapping("findAll")
//    @ApiOperation(value = "查询所有客户")
//    public List<ClientBean> findAll(){
//        return clientService.findAll();
//    }

}
